import 'package:flutter/foundation.dart';
import 'package:lib_core/lib_core.dart';

typedef GTGlobalConfigChangeListener = void Function(GTGlobalConfigEventAction action, GTGlobalConfig globalConfig);

class GTGlobalConfigChangeNotifier extends ChangeNotifier {
  // 存储监听器，方便销毁
  static final Map<String, List<GTGlobalConfigChangeListener>> _callbackMap = {};

  /// Flutter 全局配置参数，第一次 通过 Main 函数传递
  static var _globalConfig = GTGlobalConfig();

  static GTGlobalConfig get globalConfig => _globalConfig;

  static void setupGlobalConfig(GTGlobalConfig globalConfig) {
    _globalConfig = globalConfig;
  }

  /// 单例模式
  static final GTGlobalConfigChangeNotifier _instance = GTGlobalConfigChangeNotifier._internal();

  static GTGlobalConfigChangeNotifier get instance => _instance;

  // 私有的命名构造函数
  GTGlobalConfigChangeNotifier._internal() {
    // 开始监听 深色模式切换
    GTLibCoreEventChannel.addListener(GTGlobalConfigEventAction.darkMode, (actionType, actionName, data) {
      if (data == null) {
        GTLogger.debug("GTLibCoreChangeNotifier darkMode listen: The event data is null");
        return;
      }

      GTLogger.debug("GTLibCoreChangeNotifier darkMode listen: $data");

      var darkMode = data["darkMode"] ?? false;
      globalConfig.darkMode = darkMode;

      // 通知更改
      notifyChange(GTGlobalConfigEventAction.darkMode);
    });

    // 开始监听 语言切换
    GTLibCoreEventChannel.addListener(GTGlobalConfigEventAction.language, (actionType, actionName, data) {
      if (data == null) {
        GTLogger.debug("GTLibCoreChangeNotifier darkMode listen: The event data is null");
        return;
      }

      GTLogger.debug("GTLibCoreChangeNotifier language listen: $data");

      var language = data["language"] as String?;
      if (language == null || language.isEmpty) {
        return;
      }
      globalConfig.language = language;

      // 通知更改
      notifyChange(GTGlobalConfigEventAction.language);
    });

    // 开始监听 绿涨红跌
    GTLibCoreEventChannel.addListener(GTGlobalConfigEventAction.hzldMode, (actionType, actionName, data) {
      if (data == null) {
        GTLogger.debug("GTLibCoreChangeNotifier hzldMode listen: The event data is null");
        return;
      }

      GTLogger.debug("GTLibCoreChangeNotifier hzldMode listen: $data");

      var hzldMode = data["hzldMode"] ?? false;
      globalConfig.hzldMode = hzldMode;

      // 通知更改
      notifyChange(GTGlobalConfigEventAction.hzldMode);
    });
  }

  /// 通知更改
  void notifyChange(GTGlobalConfigEventAction action) {
    // 通知默认 Listener
    notifyListeners();

    // 分发具体分类监听
    var callbackList = _callbackMap[action.name] ??= [];
    // 通知自定义 listener
    for (var element in callbackList) {
      element.call(action, globalConfig);
    }
  }

  /// 方便外层直接注册监听器
  static void addChangeListener(GTGlobalConfigEventAction action, GTGlobalConfigChangeListener listener) {
    // 存储 数据方便后续删除
    var callbackList = _callbackMap[action.name] ??= [];
    callbackList.add(listener);
    _callbackMap[action.name] = callbackList;
  }

  static void addChangeListenerByList(List<GTGlobalConfigEventAction> actions, GTGlobalConfigChangeListener listener) {
    if (actions.isEmpty) {
      return;
    }
    for (var element in actions) {
      addChangeListener(element, listener);
    }
  }

  static void removeChangeListener(GTGlobalConfigEventAction action, GTGlobalConfigChangeListener listener) {
    // 清除监听器
    var callbackList = _callbackMap[action.name] ??= [];
    callbackList.remove(listener);
  }

  static void removeChangeListenerByList(List<GTGlobalConfigEventAction> actions, GTGlobalConfigChangeListener listener) {
    if (actions.isEmpty) {
      return;
    }
    for (var element in actions) {
      removeChangeListener(element, listener);
    }
  }
}
